package com.anyisn.anyflickr.shell.view
{
	import com.anyisn.anyflickr.shell.ShellFacade;
	import com.anyisn.anyflickr.shell.view.components.itemrender.PeopleTreeIR;
	import com.anyisn.anyflickr.shell.view.components.popup.PeopleWindow;
	
	import flash.events.Event;
	
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.patterns.mediator.Mediator;

	public class PeopleWindowMediator extends Mediator
	{
		public static const NAME:String = 'PeopleWindowMediator';
		
		public function PeopleWindowMediator(viewComponent:Object)
		{
			super(NAME, viewComponent);
			
			win.addEventListener(PeopleTreeIR.TILE_PHOTOS, tilePhotos);	
			win.addEventListener(PeopleTreeIR.SLIDE_PHOTOS, slidePhotos);
			win.addEventListener(PeopleTreeIR.DELETE_PERSON, del);
			win.addEventListener(PeopleTreeIR.GET_PERSON_INFO, getInfo);
			win.addEventListener(Event.CLOSE, close);
			win.addEventListener(PeopleWindow.FILTER, onFilter);
		}
		
		override public function onRegister() : void {
			sendNotification(ShellFacade.TAGS_GET_LIST, this.getMediatorName());
		}
		
		override public function listNotificationInterests():Array {
			return [
					ShellFacade.PERSON_GET_LIST_SUCCESS,
					ShellFacade.TAGS_GET_LIST_FOR_PEOPLE_WINDOW_SUCCESS
					];
		}
		
		override public function handleNotification(notification:INotification):void {
			switch(notification.getName()) {
				case ShellFacade.PERSON_GET_LIST_SUCCESS:
					var people:Array = notification.getBody() as Array;
					win.tree.dataProvider = people;
					break;
				case ShellFacade.TAGS_GET_LIST_FOR_PEOPLE_WINDOW_SUCCESS:
					var temp:Array = notification.getBody() as Array;
					var tags:Array = temp.slice();
					tags.unshift({name: '--A-L-L--', id: 0});
					if(win.tagsCB.selectedIndex==-1) {
						win.tagsCB.dataProvider = tags;
					} else {
						var hasLastTag:Boolean = false;
						for(var i:uint=0;i<tags.length;i++) {
							if(tags[i].id == win.tagsCB.selectedItem.id) {
								win.tagsCB.dataProvider = tags;
								win.tagsCB.selectedIndex = i;
								hasLastTag = true;
								break;
							}
						}
						if(!hasLastTag)
							win.tagsCB.dataProvider = tags;
					}
					onFilter();
					break;
			}
		}
		
		private function onFilter(e:Event = null):void {
			sendNotification(ShellFacade.PERSON_FILTER_BY_TAG, win.tagsCB.selectedItem.id);
		}
		
		private function getInfo(e:Event):void {
			sendNotification(ShellFacade.PEOPLE_GET_INFO, e.target.data.userid);
		}
		
		private function tilePhotos(e:Event):void {
			var nsid:String = win.tree.selectedItem.userid;
			sendNotification(ShellFacade.TILE_PEOPLE_GET_PUBLIC_PHOTOS, nsid);
		}
		
		private function slidePhotos(e:Event):void {
			var nsid:String = win.tree.selectedItem.userid;
			sendNotification(ShellFacade.SLIDE_PEOPLE_GET_PUBLIC_PHOTOS, nsid);
		}
		
		private function del(e:Event):void {
			sendNotification(ShellFacade.PERSON_DELETE, e.target.data.userid);
		}
		
		private function close(e:Event):void {
			win.removeEventListener(PeopleTreeIR.TILE_PHOTOS, tilePhotos);	
			win.removeEventListener(PeopleTreeIR.SLIDE_PHOTOS, slidePhotos);
			win.removeEventListener(PeopleTreeIR.DELETE_PERSON, del);
			win.removeEventListener(PeopleTreeIR.GET_PERSON_INFO, getInfo);
			win.removeEventListener(Event.CLOSE, close);
			win.removeEventListener(PeopleWindow.FILTER, onFilter);
			
			setViewComponent(null);
			facade.removeMediator(this.getMediatorName());
		}
		
		private function get win():PeopleWindow {
			return viewComponent as PeopleWindow;
		}
	}
}